package com.wjw.oauth2demo2.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/user")
public class userController {

    @RequestMapping("getNowUser")
    public Object geNowUser(Authentication authentication,HttpServletRequest request){
        String head=request.getHeader("authorization");
        //bearer+空格=7
        String token=head.substring(head.indexOf("bearer")+7);
        return Jwts.parser()
                //JwtTokenStoreConfig里配置密钥，转成utf-8
                .setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token).getBody();
    }
}
